package com.zshan.clinic.web.controller.patient;


import com.zshan.clinic.admin.entity.vo.PatientInfoDetailVo;
import com.zshan.clinic.admin.entity.vo.PatientInfoPageVo;
import com.zshan.clinic.admin.service.patient.ClinicPatientService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.database.model.ClinicPatient;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 患者信息
 */
@RestController
@RequestMapping("/patient")
public class PatientController extends AbstractController {


    @Autowired
    private ClinicPatientService patientService;


    /**
     * 查询患者列表
     * @param patientName 患者名
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPatientList")
    public RestResp<List<PatientInfoPageVo>> getPatientList(@RequestParam(name = "patientName", required = false) String patientName,
                                                            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<PatientInfoPageVo> patientList = patientService.getPatientList(userInfo.getClinicId(), patientName, pageNo, pageSize);
        return RestResp.SUCCESS(patientList);
    }

    /**
     * 查询患者详情
     * @param patientId 患者ID
     * @return
     */
    @GetMapping("/getPatientDetail")
    public RestResp<PatientInfoDetailVo> getPatientDetail(@RequestParam(name = "patientId") Long patientId) {
        UserInfo userInfo = getUserInfo();
        PatientInfoDetailVo patientInfoDetailVo = patientService.getPatientDetail(userInfo.getClinicId(), patientId);
        return RestResp.SUCCESS(patientInfoDetailVo);
    }

    /**
     * 添加患者
     * @param patientInfo
     * @return
     */
    @PostMapping("/addPatient")
    public RestResp<Void> addPatient(@RequestBody ClinicPatient patientInfo) {
        UserInfo userInfo = getUserInfo();
        patientInfo.setCreateUserId(userInfo.getEmpId());
        patientInfo.setCreateBy(userInfo.getEmpName());
        patientInfo.setClinicId(userInfo.getClinicId());
        patientService.addPatient(patientInfo);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑患者
     * @param patientInfo
     * @return
     */
    @PostMapping("/updatePatient")
    public RestResp<Void> updatePatient(@RequestBody ClinicPatient patientInfo) {
        UserInfo userInfo = getUserInfo();
        patientInfo.setUpdateUserId(userInfo.getEmpId());
        patientInfo.setUpdateBy(userInfo.getEmpName());
        patientInfo.setClinicId(userInfo.getClinicId());
        patientService.updatePatient(patientInfo);
        return RestResp.SUCCESS();
    }
}
